#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include "cv.h"
#include "highgui.h"

#define boolean int
#define TRUE 0
#define FALSE -1
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))

int* istogramma(IplImage *image){
	int isto[256];
	int i,j;
	int w,h,ws;

	w = image->width;
	ws= image->widthStep;
	h = image->height;

	for(j=0;j<256;j++)
		isto[j]=0;

	for(j=0; j<h; j++){//scansiono le righe
		for(i=0; i<w; i++){//ad ogni riga, scansiono le colonne 
			isto[((unsigned char*)(image->imageData))[j*ws+i]]++;
		}
	}
	return isto;
}

int* istogramma(IplImage *image,int w_iniziale,int w_finale,int h_iniziale,int h_finale){
	int isto[256];
	int i,j;
	int w,h,ws;

	w = image->width;
	ws= image->widthStep;
	h = image->height;

	for(j=0;j<256;j++)
		isto[j]=0;

	for(j=h_iniziale; j<h_finale; j++){//scansiono le righe
		for(i=w_iniziale; i<w_finale; i++){//ad ogni riga, scansiono le colonne 
			isto[((unsigned char*)(image->imageData))[j*ws+i]]++;
		}
	}
	return isto;
}



boolean equalImages(IplImage* image_1,IplImage* image_2){
	
	int i,j;
	int w1,h1,ws1;	
	int w2,h2,ws2;


	w1 = image_1->width;
	ws1= image_1->widthStep;
	h1 = image_1->height;
	w2 = image_2->width;
	ws2= image_2->widthStep;
	h2 = image_2->height;

	if(w1!=w2 || ws1!=ws2 || h1!=h2) return FALSE;

	for(j=0; j<h1; j++){//scansiono le righe
		for(i=0; i<w1; i++){//ad ogni riga, scansiono le colonne
			if(((unsigned char*)(image_1->imageData))[j*ws1+i]!=((unsigned char*)(image_2->imageData))[j*ws1+i])
				return FALSE;
		}
	}

	return TRUE;


}

int mediano(int* valori,int length){
	int* temp;
	int mezzo,i,k,corrente,val;
	int length_temp=0;

	temp=(int*)malloc(sizeof(int)*length);
	mezzo=(int)ceil((double)(((double)length)/2));

	for(i=0;i<length;i++){
		corrente=valori[i];
		for(k=0;k<length_temp;k++){
			if(corrente<temp[k])
			{
				val=corrente;
				corrente=temp[k];
				temp[k]=val;
			}
		}
		temp[k]=corrente;
		length_temp++;
	}

	return temp[mezzo];
}